﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace NSATBPortal
{
    public partial class MachineGroupEditAssignments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string sVal = Server.UrlDecode(Request.QueryString["Name"].ToString());
            lblParent.Text = "Assignments for " + sVal;

        }
        protected void DefMachineGroupGrid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int iIndex = e.NewEditIndex;

            int iMachineID = int.Parse(DefMachineGroupGrid.DataKeys[iIndex].Value.ToString());
            bool bCheckState = (DefMachineGroupGrid.Rows[iIndex].Cells[1].Text == "Yes");

            string sVal = Request.QueryString["ID"].ToString();
            int iMachineGroupID = int.Parse(sVal);

            string sUserID = Request.LogonUserIdentity.Name;
            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                objATBDB.ModifyDefMachineGroupAssignment(iMachineGroupID, iMachineID, !bCheckState, sUserID);
            }

            // Update Grid
            if (!bCheckState)
                DefMachineGroupGrid.Rows[iIndex].Cells[1].Text = "Yes";
            else
                DefMachineGroupGrid.Rows[iIndex].Cells[1].Text = "";
        }
        protected void DefMachineGroupGrid_Init(object sender, EventArgs e)
        {
            string sVal = Request.QueryString["ID"].ToString();
            int iID = Convert.ToInt32(sVal);

            DataTable dtResults;
            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                dtResults = objATBDB.GetDefMachineGroupAssignments(iID);
            }

            DefMachineGroupGrid.DataSource = dtResults;
            DefMachineGroupGrid.DataBind();
        }

    }
}